<?php
            
use app\model\Record; 
use app\model\Theme; 
use app\model\Userdata; 


/**
 * Desc:抽奖动作
 * Time: 2021/3/23 8:31 上午
 */
class GetOne_Page extends PluginMobilePage {

    function main($method)
    {
        global $_P,$_G,$_S,$_C;
        $fromtype = $_P['from'];
        if (!$_G['ispost']) {
            return_json(1,'非法请求');
        } else {
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $theme = Theme::getThemeById($themeid);
            if (!$themeid || !$theme) {
                return_json(1,'活动入口不正确');
            } else {
                $fans = $this->checkuser();
                if ($fans && $fans['uid']){
                    $fromuid = $_P['fromuid']?$_P['fromuid']:$fans['uid'];
                    //用户抽奖日志收集
                    Userdata::addLog($themeid, $fans['uid'], 18, '',$fans['workerid'],$fromuid);
                    $res = LotteryModel::LtRes($theme,$_C,$fromuid,$fans['uid']);
                    $error_txt = $res['error_txt'];
                    $has_winning = $res['has_winning'];
                    $lose_info = $res['lose_info'];
                    $has_info = $res['has_info'];

                    $lt_btn_txt = isset($res['lt_btn_txt'])?$res['lt_btn_txt']:'抽奖';
                    $remain_num = isset($res['remain_num'])?$res['remain_num']:0;
                    $tips = isset($res['tips'])?$res['tips']:'';
                    $can_lt = isset($res['can_lt'])?$res['can_lt']:false;


                    $getAlt_html = '';
                    if ($has_winning) {
                        //TODO 中奖后的弹窗
                        $getAlt_html = '<div class="getAlt">'.PHP_EOL.'<div class="getBox">'.PHP_EOL.
                            '<h2>'.$has_info['title'].'</h2>'.PHP_EOL.
                            '<div class="producePic">'.PHP_EOL.'<img src="'.$has_info['img'].'">'.PHP_EOL.'</div>'.PHP_EOL.
                            '<p>'.$has_info['name'].'-'. $has_info['name2'] . '</p>'.PHP_EOL.'<span>'.$has_info['tips'].'</span>'.PHP_EOL.'<div class="getControl">'.PHP_EOL.
                            '<a class="close">继续抽奖</a>'.PHP_EOL.'<a class="convert myGet"  data-pid="'.$has_info['p_id'].'" data-ck="'.$has_info['checked_key'].'">去领取</a>'.PHP_EOL.'</div>'.PHP_EOL.'</div>'.PHP_EOL.'<span class="close"></span>'.PHP_EOL.'</div>'.PHP_EOL;
                    }


                    $loseAlt_html = '';
                    if (!$has_winning) {
                        //TODO 未中奖弹窗
                        $loseAlt_html = '<div class="loseAlt">'.PHP_EOL.'<div class="loseBox">'.PHP_EOL.'<h2>'.$lose_info['title'].'</h2>'.PHP_EOL.'<p>'.$lose_info['content'].'</p>'.PHP_EOL.
                            '<img src="'.$lose_info['icon'].'">'.PHP_EOL.'<div class="controlBox">'.PHP_EOL.
                            '<a href="javascript:void(0)" class="iknowBtn">'.$lose_info['btn_txt'].'</a>'.PHP_EOL.'</div>'.PHP_EOL.'</div>'.PHP_EOL.'<span class="close"></span>'.PHP_EOL.'</div>'.PHP_EOL;
                    }

                    //判断用户是否存在订单
                    $themyrecord = Record::getLastGroupByUid($fans['uid'],$themeid);
                    $my_prize = LotteryModel::getMyPrizeInfo($themeid,$_C,$fans['uid']);
                    $open_red_package = $_C['yx_lt_open_red_package'];
                    $record_list = LotteryModel::getRecordList($themeid,$_C,$fans['nickname']);//中奖记录
                    $method_list = LotteryModel::getLotteryMethod($fans['uid'],$themeid,$_C,$themyrecord);//抽奖攻略

                    //TODO 弹出我的奖品
                    $myWinner_html = '<div class="myWinner">'.PHP_EOL.'<span class="winClose"></span>'.PHP_EOL.'<div class="winnerChose">'.PHP_EOL.'<span>中奖纪录</span>'.PHP_EOL.
                        '<span class="cur">我的奖品</span>'.PHP_EOL.'<span>抽奖攻略</span>'.PHP_EOL.'</div>'.PHP_EOL.'<div class="winerChosen">'.PHP_EOL;
                    //  --中奖记录
                    $yx_peopleList_html = '<div class="yx_choseBar yx_peopleList dn">'.PHP_EOL.'<ul>'.PHP_EOL;

                    if ($record_list){
                        foreach ($record_list as $key => $val) {
                            $yx_peopleList_html .= '<li>'.PHP_EOL.'<div class="peoImg">'.PHP_EOL.'<img src="';
                            $yx_peopleList_html .= $val['avatar'].'">'.PHP_EOL.'</div>'.PHP_EOL.'<div class="peoName">'.PHP_EOL;
                            $yx_peopleList_html .= $val['username'].'</div>'.PHP_EOL.'<div class="peoInfor">'.PHP_EOL;
                            $yx_peopleList_html .= $val['tips'].'</div>'.PHP_EOL.'<div class="peoTime">'.PHP_EOL;
                            $yx_peopleList_html .= '<p>'.substr($val['created_at'],0,10).'</p>'.PHP_EOL.'<p>'.substr($val['created_at'],10,9).'</p>'.PHP_EOL.' </div>'.PHP_EOL.'</li>';
                        }
                    }
                    $yx_peopleList_html.= '</ul>'.PHP_EOL.'</div>'.PHP_EOL;

                    // --我的奖品  红包加抽奖奖品
                    $my_prize_html = '<div class="yx_choseBar">'.PHP_EOL.'<div class="ticeketList">';
                    //判断存在红包
                    if ($open_red_package) {
                        $my_prize_html .= '<div class="redBag">'.PHP_EOL.'<div class="redbagTxt">'.PHP_EOL.
                            '<p>'.$my_prize['red_package_info']['amount'].'</p>'.PHP_EOL.
                            '<span>'. $my_prize['red_package_info']['btm_tips']. '</span>'.PHP_EOL.'</div>'.PHP_EOL.
                            '<a href="" data-cando="'. $my_prize['red_package_info']['can_get'] . '" class="getBag">'.
                            $my_prize['red_package_info']['btn_txt']. '</a>'.PHP_EOL.'</div>'.PHP_EOL;
                    }
                    //---默认奖品
                    $my_prize_html.='<ul>'.PHP_EOL;
                    if ($my_prize['prize_info']) {
                        foreach ($my_prize['prize_info'] as $t => $item) {
                            $my_prize_html.='<li>'.PHP_EOL.'<div class="ticketInfor">'.PHP_EOL.'<div class="ticketProduct">'.PHP_EOL.
                                '<img src="'. $item['img'] . '">'.PHP_EOL.'</div>'.PHP_EOL.'<div class="getDate">'.PHP_EOL.'<p>'.$item['name'].'</p>'.PHP_EOL.
                                '<span>'.$item['time'].'</span>'.PHP_EOL.'</div>'.PHP_EOL;
                            if ($item['status']==1) {
                                if ($item['default_type']!=2) {
                                    $my_prize_html .= '<a  data-status="'.$item['status'].'" onclick="convert(this)" data-fid="'.$item['f_id'].'">'.$item['btn_txt'].'</a>'.PHP_EOL;
                                }
                            } else {
                                $my_prize_html .= '<a href="" data-status="'.$item['status'].'" data-fid="'.$item['f_id'].'">'.$item['btn_txt'].'</a>'.PHP_EOL;
                            }
                            $my_prize_html.= '</div>'.PHP_EOL.'</li>'.PHP_EOL;
                        }
                    }
                    $my_prize_html.='</ul>'.PHP_EOL;
                    $my_prize_html.='<a href="" class="continue">'.$my_prize['btm_txt'].'</a>'.PHP_EOL.'</div>'.PHP_EOL.'</div>'.PHP_EOL;


                    // --抽奖攻略
                    $strategy_html = '<div class="yx_choseBar strategy dn">'.PHP_EOL.'<ul>'.PHP_EOL;
                    if ($method_list) {
                        foreach ($method_list as $K => $val) {
                            if ($val['type']!='timeset') {

                                if ($val['type']=='order') {
                                    $strategy_html.= '<li>'.PHP_EOL.'<img src="'. $val['icon']. '">'.PHP_EOL.'<div class="statTxt">'.PHP_EOL.
                                        '<h2>'.$val['title'].PHP_EOL.'<span>'.$val['num'].'</span>'.PHP_EOL.'</h2>'.PHP_EOL.
                                        '<p>'.$val['tips'].'</p>'.PHP_EOL.'</div>'.PHP_EOL.
                                        '<a href="javascript::" onclick="show_register_info()" data-status="'.$val['status'].'" data-type="'. $val['type']. '" data-timeset="'. $val['timeset']. '" data-otherid="'.$val['other_id'].'" class="stratTag">'.
                                        $val['btn_txt'].'</a>'.PHP_EOL.'</li>'.PHP_EOL;
                                } elseif ($val['type']=='share' || $val['type']=='share_moments') {
                                    $strategy_html.= '<li>'.PHP_EOL.'<img src="'. $val['icon']. '">'.PHP_EOL.'<div class="statTxt">'.PHP_EOL.
                                        '<h2>'.$val['title'].PHP_EOL.'<span>'.$val['num'].'</span>'.PHP_EOL.'</h2>'.PHP_EOL.
                                        '<p>'.$val['tips'].'</p>'.PHP_EOL.'</div>'.PHP_EOL.
                                        '<a href="javascript::" onclick="show_share()" data-status="'.$val['status'].'" data-type="'. $val['type']. '" data-timeset="'. $val['timeset']. '" data-otherid="'.$val['other_id'].'" class="stratTag">'.
                                        $val['btn_txt'].'</a>'.PHP_EOL.'</li>'.PHP_EOL;

                                } else {
                                    $strategy_html.= '<li>'.PHP_EOL.'<img src="'. $val['icon']. '">'.PHP_EOL.'<div class="statTxt">'.PHP_EOL.
                                        '<h2>'.$val['title'].PHP_EOL.'<span>'.$val['num'].'</span>'.PHP_EOL.'</h2>'.PHP_EOL.
                                        '<p>'.$val['tips'].'</p>'.PHP_EOL.'</div>'.PHP_EOL.
                                        '<a href="" data-status="'.$val['status'].'" data-type="'. $val['type']. '" data-timeset="'. $val['timeset']. '" data-otherid="'.$val['other_id'].'" class="stratTag">'.
                                        $val['btn_txt'].'</a>'.PHP_EOL.'</li>'.PHP_EOL;
                                }
                            } else {
                                $strategy_html.= '<li>'.PHP_EOL.'<img src="'.$val['icon'].'">'.PHP_EOL.'<div class="statTxt">'.PHP_EOL;
                                $strategy_html.= '<h2>'.$val['title'].PHP_EOL.'<span>'.$val['num'].'</span>'.PHP_EOL.'</h2>'.PHP_EOL.
                                    '<p>'.$val['tips'].'</p>'.PHP_EOL.'</div>'.PHP_EOL;
                                //判断时间戳
                                $temp_btn_txt = $val['btn_txt']?$val['btn_txt']:'后可领';
                                $strategy_html.= '<a href="" data-status="'.$val['status'].'" data-type="'.$val['type'].'" data-timeset="'.$val['timeset'].'"
                               data-otherid="'.$val['other_id'].'" class="stratTag timeEnd">'.PHP_EOL.'<div class="yomibox mobile_color" data="'.$val['timeset'].'"></div>'.PHP_EOL.
                                    $temp_btn_txt.'</a>'.PHP_EOL.'</li>'.PHP_EOL;
                            }
                        }
                    }
                    $strategy_html.='</ul>'.PHP_EOL.'</div>'.PHP_EOL;

                    $myWinner_html.= $yx_peopleList_html.$my_prize_html.$strategy_html.'</div>'.PHP_EOL.'</div>'.PHP_EOL;

                    //兑换信息
                    $my_convert_info = [
                        'prize_info'=>$my_prize['prize_info'],
                        'convert_title_prefix'=>'中奖啦，恭喜您抽中了',
                        'yx_lt_convert_method'=>"【兑奖方式】".$_C['yx_lt_convert_method'],
                        'yx_lt_convert_tel'=>"电话：".$_C['yx_lt_convert_tel'],
                        'yx_send_people'=>"  发奖人：".$_C['yx_send_people'],
                        'yx_send_mobile'=>"联系方式：".$theme['mobile'],
                        'yx_btn_txt'=>'已到店出示核销二维码',
                        'yx_btm_tips'=>"温馨提示：请妥善保管兑奖凭证，用户可截图保存",
                    ];


                    if ($fromtype=='mobile') {
                        if (!$can_lt && !$tips){
                            $tips = '还剩'.$remain_num.'次';
                        }
                        $format_res = [
                            'getAlt_html'=>Theme::encode($getAlt_html),
                            'loseAlt_html'=>Theme::encode($loseAlt_html),
                            'myWinner_html'=>Theme::encode($myWinner_html),
                            'my_convert_info'=>$my_convert_info,
                            'error_txt'=>$error_txt,
                            'has_winning'=>$has_winning,
                            'lose_info'=>$lose_info,
                            'has_info'=>$has_info,
                            'lt_btn_txt'=>$lt_btn_txt,
                            'remain_num'=>$remain_num,
                            'show_tips'=>$tips,
                            'can_lt'=>$can_lt,
                        ];
                    } elseif ($fromtype=='wxapp') {
                        $format_res = [
                            'my_convert_info'=>$my_convert_info,
                            'error_txt'=>$error_txt,
                            'has_winning'=>$has_winning,
                            'lose_info'=>$lose_info,
                            'has_info'=>$has_info,
                            'lt_btn_txt'=>$lt_btn_txt,
                            'remain_num'=>$remain_num,
                            'show_tips'=>$tips,
                            'can_lt'=>$can_lt,
                        ];
                    }
                    //更新提示标志位
                    LotteryModel::getOldOneTip($themeid,$fans['uid'],$fans,true);
                    return_json(0,'操作成功',$format_res);
                }else{
                    $format_res = [
                        'my_convert_info'=>[],
                        'error_txt'=>'发生了意外',
                        'has_winning'=>false,
                        'lose_info'=>[],
                        'has_info'=>[],
                        'lt_btn_txt'=>'',
                        'remain_num'=>0,
                        'show_tips'=>'',
                        'can_lt'=>false,
                    ];
                    return_json(1,'操作失败',$format_res);
                }
            }
            return_json(1,'操作失败');
        }
    }
}